var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'soc_master_ajs.html', controller: ListExpMstCtrl}).
      when('/delete/', {templateUrl: 'soc_master_ajs.html', controller: ListExpMstCtrl}).      
      when('/updte/', {templateUrl: 'soc_master_ajs.html', controller: ListExpMstCtrl}). 
      when('/cancel/', {templateUrl: 'soc_master_ajs.html', controller: ListExpMstCtrl}). 
      otherwise({redirectTo: '/'});
}]);


function ListExpMstCtrl($scope, $http,$window,$location,$filter)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 25;
	$scope.Math = window.Math;
	$scope.ed = false;
	$scope.n_arr = [];
	
    $http.get('socdata.php').success(function(data) {
		$scope.socmast = data;
		for(var i=0; i<data.length;i++)
			$scope.n_arr[i] = data[i].name;
    });

    //Function to Edit Record
    $scope.edit = function(id) {
         for(i in $scope.socmast) {
			if($scope.socmast[i].id == id)
            {          
               $scope.newsocmast = angular.copy($scope.socmast[i]);				
            }
        }
		$scope.ed = true;
    };
    
	$scope.list=function()
	{
		$(function () 
		{				
			$( "#soc" ).autocomplete({
				source: $scope.n_arr
			});				
		});
	}
	
    //Function to Delete Record
   $scope.dlt = function(newsocmast) {
      if (confirm('Do you really want to delete this Society record?')) 
        {
            var sql = 'soc_cud.php?'+"id="+newsocmast.id+"&cud=d";				
			//$window.alert( sql); 
            $http.post(sql).success(function(data)
            {                               
				alert("Record "+newsocmast.id+" deleted successfully!");				
				//$scope.cancel();
            });             
        }
      };
      
	
		//Function to Update Data 
		$scope.upd = function(newsocmast) 
			{   					
				$scope.newsocmast.name=document.getElementById("soc").value;
				if(!newsocmast.name)
					alert("Please enter the name");
				else{		
					if(!newsocmast.zip)
						newsocmast.zip='';
					if(!newsocmast.addr)
						newsocmast.addr='';
					if(!newsocmast.road)
						newsocmast.road='';
					if(!newsocmast.locality)
						newsocmast.locality='';
					if(!newsocmast.district)
						newsocmast.district='';						
					var sql = 'soc_cud.php?'+"id="+newsocmast.id+"&name="+$filter('uppercase')(newsocmast.name)+ "&addr="+$filter('uppercase')(newsocmast.addr)+"&zip="+newsocmast.zip+"&road="+$filter('uppercase')(newsocmast.road)+"&locality="+$filter('uppercase')(newsocmast.locality)+"&district="+$filter('uppercase')(newsocmast.district)+"&cud=u";	
					$http.post(sql).success(function(data)  
					{					
						alert("Record "+newsocmast.id+" updated successfully!");							
						//$scope.cancel();						
					});					
					
				}
			};
      
	 //Function to Insert Data 
	   $scope.sv = function(newsocmast) 
	  	{   
			$scope.newsocmast.name=document.getElementById("soc").value;
            if(!newsocmast.name)
				alert("Please enter the name");
			else {
				var chk=false;
				$http.get('socdata.php').success(function(data) {
					$scope.socmast = data;
					newsocmast.name = $filter('uppercase')(newsocmast.name);
					for(var i=0;i<$scope.socmast.length;i++)
						if($scope.socmast[i].name == newsocmast.name)
							chk=true;
					if(chk==true)
						alert("This society already exists");
					else{
						if(!newsocmast.zip)
							newsocmast.zip='';
						if(!newsocmast.addr)
							newsocmast.addr='';
						if(!newsocmast.road)
							newsocmast.road='';
						if(!newsocmast.locality)
							newsocmast.locality='';
						if(!newsocmast.district)
							newsocmast.district='';
						var sql = 'soc_cud.php?'+"id="+newsocmast.id+"&name="+$filter('uppercase')(newsocmast.name)+ "&addr="+$filter('uppercase')(newsocmast.addr)+"&zip="+newsocmast.zip+"&road="+$filter('uppercase')(newsocmast.road)+"&locality="+$filter('uppercase')(newsocmast.locality)+"&district="+$filter('uppercase')(newsocmast.district)+"&cud=c";							
						$http.post(sql).success(function(data)  
						{										
							alert("Record saved successfully!");				
							$scope.cancel();
						});													
					}
				});					
			}
		};
	  
	  //Function to Clear 
        $scope.cancel= function()
		{
			$scope.activePath = null;
			$scope.activePath = $location.path('soc_master_ajs.php');
		};
      
 }
    
        //Custom Filter Function to sort the record.
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
	
	app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
	
	
 app.directive('myMaxlength', function() {
  return {
    require: 'ngModel',
    link: function (scope, element, attrs, ngModelCtrl) {
      var maxlength = Number(attrs.myMaxlength);
      function fromUser(text) {
          if (text.length > maxlength) {
            var transformedInput = text.substring(0, maxlength);
            ngModelCtrl.$setViewValue(transformedInput);
            ngModelCtrl.$render();
			alert("character limit reached...");
            return transformedInput;			
          } 
          return text;
      }
      ngModelCtrl.$parsers.push(fromUser);
    }
  }; 
});

        